﻿﻿@{
 ViewBag.Title = "Creating a ASP MVC CRUD application";
 Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Creating a ASP MVC CRUD application</h2>
<h3>Creation of applicant model</h3>
<p>With our database and table created, let’s create our model. Model’s in  MVC  is a class that will get and set data for each column from our database table. Follow these steps:</p>
<ol>
<li>In Visual Studio, from Solution Explorer, right click on Models and choose Add : Class:</li>
</ol>
<p align="center"><a href="@Url.Content("~/content/aspmvccrud/images/add_model_class.jpg")" data-lightbox="Add Model Class"><img src="@Url.Content("~/content/aspmvccrud/images/thumbs/add_model_class.jpg")" alt="Add Model Class" width="300" height="90" /></a></p>
<ol start="2">
<li>
In Add New Item window:
<ol type="a">
<li>Leave Visual C# selected</li>
<li>Leave Class selected</li>
<li>In the name text field type Applicant.cs </li>
<li>Left click Add</li>
</ol>
</li>
</ol>
<p align="center"><a href="@Url.Content("~/content/aspmvccrud/images/model_class_details.jpg")" data-lightbox="Model Class Details"><img src="@Url.Content("~/content/aspmvccrud/images/thumbs/model_class_details.jpg")" alt="Model Class Details" width="300" height="228" /></a></p>
<p>In our model class add the following:</p>
<pre class="brush:csharp highlight:[5,10]">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Applicant.Models
{
public class Applicants
{
    [Key]
    public int ApplicantId { get; set;}
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
}
} 
</pre>
<p>As you can see from the code:</p>
<ul class="indent">
<li>We import a .NET library (namespace) for assigning keys in entity framework to a primary key from our database table (System.ComponentModel.DataAnnotations)</li>
<li>Create six matching public class variables with corresponding data type</li>
</ul>
<p>Save your model class and close the file.</p>
<h3>Creation of data context</h3>
<p>When working with entity framework in an  MVC  application we create a data context class that uses a data type specific to entity framework which is then type casted to our model class. Follow these steps:</p>
<p>In Visual Studio, from Solution Explorer:</p>
<ol>
<li>Right click the solution Applicant, and choose Add : New Folder:</li>
</ol>
<p align="center"><a href="@Url.Content("~/content/aspmvccrud/images/add_folder_solution_explorer.jpg")" data-lightbox="Add Folder Solution Explorer"><img src="@Url.Content("~/content/aspmvccrud/images/thumbs/add_folder_solution_explorer.jpg")" alt="Add Folder Solution Explorer" width="300" height="171" /></a></p>
<ol start="2">
<li>Name the folder DAL (data access layer):</li>
</ol>
<p align="center"><img src="@Url.Content("~/content/aspmvccrud/images/dal_folder.jpg")" alt="DAL folder" width="156" height="152" /></p>
<ol start="3">
<li>Right click DAL and choose Add : Class:</li>
</ol>
<p align="center"><a href="@Url.Content("~/content/aspmvccrud/images/add_dal_class.jpg")" data-lightbox="Add DAL Class"><img src="@Url.Content("~/content/aspmvccrud/images/thumbs/add_dal_class.jpg")" alt="Add DAL Class" width="300" height="171" /></a></p>
<ol start="4">
<li>
In Add New Item window:
<ol type="a">
<li>Leave Visual C# selected</li>
<li>Choose Class</li>
<li>In the name text field, type ApplicantDataContext.cs</li>
<li>Left click Add:</li>
</ol>
</li>
</ol>
<p align="center"><a href="@Url.Content("~/content/aspmvccrud/images/choose_dal_name.jpg")" data-lightbox="Choose DAL Name"><img src="@Url.Content("~/content/aspmvccrud/images/thumbs/choose_dal_name.jpg")" alt="Choose DAL Name" width="300" height="227" /></a></p>
<p>Add the following to our class:</p>
<pre class="brush:csharp highlight:[5,6,9,11]">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Applicant.Models;
using System.Data.Entity;
namespace Applicant.DAL
{
public class ApplicantDataContext : DbContext
{
    public DbSet&lt;Applicants&gt; Applicant { get; set; }
}
} 
</pre>
<p>As you can see from the code:</p>
<ul class="indent">
<li>
We import two libraries (name spaces):
<ul>
<li>Our applicant model class (using Applicant.Models)</li>
<li>A .NET library for working with entity framework (using System.Data.Entity)</li>
</ul>
</li>
<li>
We make the class signature inherit from DbContext, a specific class from entity framework which gets translated internally to our web.config file &lt;connectionstring&gt; XML node
<ul>
<li>Inside the constructor class we create a public applicant variable which is typed cast to our model class, applicants, with a data type DbSet, which makes entity framework capable of getting and setting specific column types</li>
</ul>
</li>
</ul>
<p>Save your data context class and close the file.</p>
<h3>Adding data context key to web.config</h3>
<p>Once we’ve added a data context class, we need to instruct our class where the database server resides. Let’s modify the file</p>
<ul class="indent">
<li>In Visual Studio, from Solution Explorer, scroll down and double click Web.config</li>
<li>In Web.config, look for an xml tag named connectionString</li>
<li>Inside the parent node, we’ll see a key added for applicant context, replace that key with the following:</li>
</ul>
<pre class="brush:xml">
&lt;connectionstrings&gt;
&lt;add name="ApplicantDataContext" connectionstring="Data Source=(local); Initial Catalog=Applicant; Integrated Security=True;"
  providername="System.Data.SqlClient" /&gt;
&lt;/connectionstrings&gt;
</pre>
<p>As you can see from the XML markup:</p>
<ul class="indent">
<li>Add name’s value must match the class name of our data context</li>
<li>Connection string’s value must be set to either a local or remote database server with a corresponding table named Applicant</li>
</ul>
<p>Save web.config and close the file.</p>
<p>We'll continue with @Html.ActionLink("creation of our controller", "Index4").</p>